package com.yang.service.wximpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.bean.MarketComment;
import com.yang.bean.MarketCommentExample;
import com.yang.bean.MarketUser;
import com.yang.bean.common.CommonData;
import com.yang.bean.yvo.wx.CommentCountVo;
import com.yang.bean.yvo.wx.CommentLIstBO;
import com.yang.bean.yvo.wx.CommentListVO;
import com.yang.bean.yvo.wx.UserInfoDTO;
import com.yang.mapper.MarketCommentMapper;
import com.yang.mapper.MarketUserMapper;
import com.yang.service.wx.WXCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author: 杨庆
 * @since: 2022.9.12 16:30
 * @description:
 */
@Service
public class WXCommentServiceImpl implements WXCommentService {
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public CommonData selectList(CommentLIstBO commentLIstBO) {
        //TODO  获取评论
        PageHelper.startPage(commentLIstBO.getPage(),commentLIstBO.getLimit());
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();

        criteria.andValueIdEqualTo(commentLIstBO.getValueId());
        int type = commentLIstBO.getType();
        criteria.andTypeEqualTo(((byte) type));
        if (commentLIstBO.getShowType() == 1) {
            criteria.andHasPictureEqualTo(true);
        }
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(example);
        ArrayList<CommentListVO> list = new ArrayList<>();
        for (MarketComment marketComment : marketComments) {
            CommentListVO commentListVO = new CommentListVO();
            commentListVO.setAddTime(marketComment.getAddTime());
            commentListVO.setPicList(Arrays.asList(marketComment.getPicUrls()));
            commentListVO.setContent(marketComment.getContent());
            commentListVO.setAdminContent(marketComment.getAdminContent());

            //TODO 根据userid查询用户信息
            MarketUser marketUser = marketUserMapper.selectByPrimaryKey(marketComment.getUserId());
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            userInfoDTO.setAvatarUrl(marketUser.getAvatar());
            userInfoDTO.setNickName(marketUser.getNickname());
            commentListVO.setUserInfo(userInfoDTO);
            list.add(commentListVO);
        }
        PageInfo<CommentListVO> commentListVOPageInfo = new PageInfo<>(list);
        CommonData data = CommonData.data(commentListVOPageInfo);
        return data;
    }

    @Override
    public CommentCountVo count(Integer valueId, Integer type) {

        //查询allCount
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = commentExample.createCriteria().andValueIdEqualTo(valueId).andTypeEqualTo(type.byteValue());

        long allCount = marketCommentMapper.countByExample(commentExample);

        //查询hasPicCount
        criteria.andPicUrlsNotEqualTo("[]");
        long hasPicCount = marketCommentMapper.countByExample(commentExample);

        CommentCountVo countVo = new CommentCountVo();
        countVo.setAllCount((int) allCount);
        countVo.setHasPicCount((int) hasPicCount);

        return countVo;
    }

    public MarketComment postComment(MarketComment marketComment, MarketUser marketUser) {
        //获取用户信息
        marketComment.setUserId(marketUser.getId());
        marketComment.setAddTime(new Date());
        marketComment.setDeleted(false);
        marketCommentMapper.insertSelective(marketComment);
        return marketComment;
    }
}
